﻿using System.Collections.Concurrent;

namespace WeWork.Service.Framework.Cache
{
    public class ConcurrentCache<TKey, TValue>
    {
        private readonly ConcurrentDictionary<TKey, TValue> _cache = new ConcurrentDictionary<TKey, TValue>();

        public TValue TryGet(TKey key)
        {
            TValue value;
            if (_cache.TryGetValue(key, out value))
            {
                return value;
            }
            return default;
        }
        
        public bool TryAdd(TKey key, TValue value)
        {
            return _cache.TryAdd(key, value);
        }

        public bool TryRemove(TKey key, out TValue value)
        {
            if (_cache.TryRemove(key, out var item))
            {
                value = item;
                return true;
            }
            value = default;
            return false;
        }
    }
}
